/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.inventory;

import com.xtd.facade.admin.SystemPropertyFacade;
import com.xtd.facade.inventory.CommodityFacade;
import com.xtd.model.employment.DepartmentPost;
import com.xtd.model.inventory.Commodity;
import com.xtd.model.inventory.CommodityHasCommodity;
import com.xtd.model.inventory.CommodityRoyalty;
import com.xtd.util.CommodityPropertyEnum;
import com.xtd.view.AbstractManager;
import com.xtd.view.ApplicationBean;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class CommodityManager extends AbstractManager {

    @EJB
    CommodityFacade commodityFacade;
    @EJB
    SystemPropertyFacade systemPropertyFacade;
    private List<Commodity> commodityList;
    private Commodity selectedCommodity;
    private String searchContent;
    private CommodityPropertyEnum commodityProperty;
    private String commodityType;
    private List<SelectItem> commodityTypeItems;
    private List<SelectItem> commodityTypeItemsForEdit;
    private List<SelectItem> packageCommodityItems;
    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;
    private int selectedCommodityId;
    private List<Commodity> commodityListForSelection;
    private DepartmentPost selectedDepartmentPost;

    /** Creates a new instance of CommodityManager */
    public CommodityManager() {
    }

    /**
     * @return the commodityList
     */
    public List<Commodity> getCommodityList() {
        if (commodityList == null) {
            commodityList = new ArrayList();
          
        }
        return commodityList;
    }

    /**
     * @param commodityList the commodityList to set
     */
    public void setCommodityList(List<Commodity> commodityList) {
        this.commodityList = commodityList;
    }

    /**
     * @return the selectedCommodity
     */
    public Commodity getSelectedCommodity() {
        return selectedCommodity;
    }

    /**
     * @param selectedCommodity the selectedCommodity to set
     */
    public void setSelectedCommodity(Commodity selectedCommodity) {
        this.selectedCommodity = selectedCommodity;
    }

    /**
     * @return the searchContent
     */
    public String getSearchContent() {
        return searchContent;
    }

    /**
     * @param searchContent the searchContent to set
     */
    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    public void seachByContent(ActionEvent actionEvent) {
        this.commodityList.clear();
        this.commodityList.addAll(this.commodityFacade.search(searchContent, commodityProperty, commodityType));

    }

    public void searchByProperty() {

        this.commodityList.clear();
        this.commodityList.addAll(this.commodityFacade.seach(commodityProperty));

    }

    public void searchByPropertyForSelection() {

        this.commodityListForSelection.clear();
        this.commodityListForSelection.addAll(this.commodityFacade.seach(commodityProperty));

    }

    public void searchByType() {
        this.commodityList.clear();
        this.commodityList.addAll(this.commodityFacade.seach(commodityProperty, commodityType));
    }

    public void searchByTypeForSelection() {
        this.commodityListForSelection.clear();
        this.commodityListForSelection.addAll(this.commodityFacade.seach(commodityProperty, commodityType));
    }

    public void searchInadequateCommodities() {
        this.commodityListForSelection.clear();
        this.commodityListForSelection.addAll(this.commodityFacade.searchInadequateCommodities());
    }

    public void cancellCreate(CloseEvent actionEvent) {
        addMessage("取消保存", null);
    }

    /**
     * @return the commodityProperty
     */
    public CommodityPropertyEnum getCommodityProperty() {
        return commodityProperty;
    }

    /**
     * @param commodityProperty the commodityProperty to set
     */
    public void setCommodityProperty(CommodityPropertyEnum commodityProperty) {
        this.commodityProperty = commodityProperty;
    }

    /**
     * @return the commodityTypeItems
     */
    public List<SelectItem> getCommodityTypeItems() {
        if (commodityProperty != null) {
            commodityTypeItems = applicationBean.getCommodityTypeItemMap().get(commodityProperty);
        }
        return commodityTypeItems;
    }

    /**
     * @param commodityTypeItems the commodityTypeItems to set
     */
    public void setCommodityTypeItems(List<SelectItem> commodityTypeItems) {
        this.commodityTypeItems = commodityTypeItems;
    }

    /**
     * @return the applicationBean
     */
    public ApplicationBean getApplicationBean() {
        return applicationBean;
    }

    /**
     * @param applicationBean the applicationBean to set
     */
    public void setApplicationBean(ApplicationBean applicationBean) {
        this.applicationBean = applicationBean;
    }

    /**
     * @return the commodityType
     */
    public String getCommodityType() {
        return commodityType;
    }

    /**
     * @param commodityType the commodityType to set
     */
    public void setCommodityType(String commodityType) {
        this.commodityType = commodityType;
    }

    public void createMaterial(ActionEvent actionEvent) {
        selectedCommodity = new Commodity(CommodityPropertyEnum.材料商品);
        commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());

    }

    public void createRetail(ActionEvent actionEvent) {
        selectedCommodity = new Commodity(CommodityPropertyEnum.零售商品);
        commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());


    }

    public void createWorking(ActionEvent actionEvent) {
        selectedCommodity = new Commodity(CommodityPropertyEnum.加工商品);
        commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());
    }

    public void createPackage(ActionEvent actionEvent) {
        selectedCommodity = new Commodity(CommodityPropertyEnum.套系商品);
        commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());

    }

    public void editPackage(ActionEvent actionEvent) {
        selectedCommodity = null;
        selectedCommodityId = 0;
    }

    public void createVirtual(ActionEvent actionEvent) {
        selectedCommodity = new Commodity(CommodityPropertyEnum.虚拟商品);
        commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());


    }

    public void save(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean isNew = selectedCommodity.isNew();

        try {
            commodityFacade.update(selectedCommodity);
            addMessage("保存成功", null);
            context.addCallbackParam("success", true);
            if (isNew) {
                commodityList.add(selectedCommodity);
                if (selectedCommodity.getProperty().equals(CommodityPropertyEnum.套系商品)) {
                    packageCommodityItems = null;
                }
            }
        } catch (Exception e) {

            getLogger(this.getClass()).log(Level.FINEST, "商品设置更新失败", e);
            addMessage("保存失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
        }
    }

    public void checkUniqueCommoditName(FacesContext context, UIComponent component, Object newValue) {
        if (!commodityFacade.checkUnique(selectedCommodity, "name", (String) newValue, selectedCommodity.isNew())) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "保存失败", "商品名称有重复"));
        }
    }

    public void checkUniqueCommoditNumber(FacesContext context, UIComponent component, Object newValue) {
        if (!commodityFacade.checkUnique(selectedCommodity, "number", (String) newValue, selectedCommodity.isNew())) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "保存失败", "商品编码有重复"));
        }
    }

    /**
     * @return the commodityTypeItemsForEdit
     */
    public List<SelectItem> getCommodityTypeItemsForEdit() {
        if (selectedCommodity != null) {
            commodityTypeItemsForEdit = applicationBean.getCommodityTypeItemMap().get(selectedCommodity.getProperty());
        }
        return commodityTypeItemsForEdit;
    }

    /**
     * @param commodityTypeItemsForEdit the commodityTypeItemsForEdit to set
     */
    public void setCommodityTypeItemsForEdit(List<SelectItem> commodityTypeItemsForEdit) {

        this.commodityTypeItemsForEdit = commodityTypeItemsForEdit;
    }

    /**
     * @return the packageCommodityItems
     */
    public List<SelectItem> getPackageCommodityItems() {
        if (packageCommodityItems == null) {
            packageCommodityItems = new ArrayList();
            List<Commodity> commodities = commodityFacade.seach(CommodityPropertyEnum.套系商品);
            packageCommodityItems.add(new SelectItem(null, "选择套系"));
            for (Commodity c : commodities) {
                packageCommodityItems.add(new SelectItem(c, c.toString()));
            }
        }
        return packageCommodityItems;
    }

  

    /**
     * @param packageCommodityItems the packageCommodityItems to set
     */
    public void setPackageCommodityItems(List<SelectItem> packageCommodityItems) {
        this.packageCommodityItems = packageCommodityItems;
    }

    public void selectPackageCommodity() {
        selectedCommodity = this.commodityFacade.find(selectedCommodityId);

    }

    /**
     * @return the selectedCommodityId
     */
    public int getSelectedCommodityId() {
        return selectedCommodityId;
    }

    /**
     * @param selectedCommodityId the selectedCommodityId to set
     */
    public void setSelectedCommodityId(int selectedCommodityId) {
        this.selectedCommodityId = selectedCommodityId;
    }

    /**
     * @return the deleteSubcommodity
     */
    public String deleteSubcommodity(CommodityHasCommodity c) {
        selectedCommodity.getSubCommodityCollection().remove(c);
        return null;
    }

    /**
     * @return the deleteSubcommodity
     */
    public String editCommodity(Commodity commodity) {
        print("edit commodity" + commodity);
        selectedCommodity = commodity;
        return null;
    }

    /**
     * @return the deleteSubcommodity
     */
    public String addSubcommodity(Commodity c) {

        if (selectedCommodity == null) {
            addMessage("添加失败", "请选定套系商品", FacesMessage.SEVERITY_ERROR);
            return null;
        }
        for (CommodityHasCommodity chc : selectedCommodity.getSubCommodityCollection()) {
            if (chc.getSubcommodity().equals(c)) {
                addMessage("添加失败", "不可重复添加", FacesMessage.SEVERITY_ERROR);
                return null;
            }
        }
        print("selectedCommodity = " + selectedCommodity);
        print("c = " + c);
        selectedCommodity.getSubCommodityCollection().add(new CommodityHasCommodity(selectedCommodity, c));
        return null;
    }

    /**
     * @return the commodityListForSelection
     */
    public List<Commodity> getCommodityListForSelection() {
        if (commodityListForSelection == null) {
            commodityListForSelection = new ArrayList();
        }
        return commodityListForSelection;
    }

    /**
     * @param commodityListForSelection the commodityListForSelection to set
     */
    public void setCommodityListForSelection(List<Commodity> commodityListForSelection) {
        this.commodityListForSelection = commodityListForSelection;
    }

    /**
     * @return the selectedDepartmentPost
     */
    public DepartmentPost getSelectedDepartmentPost() {
        return selectedDepartmentPost;
    }

    /**
     * @param selectedDepartmentPost the selectedDepartmentPost to set
     */
    public void setSelectedDepartmentPost(DepartmentPost selectedDepartmentPost) {
        this.selectedDepartmentPost = selectedDepartmentPost;
    }

    public void addCommodityRoyalty() {
        if (selectedDepartmentPost == null) {
            addMessage("添加失败", "请选定部门职位", FacesMessage.SEVERITY_WARN);
            return;
        } else {
            if (!selectedCommodity.getCommodityRoyaltyCollection().isEmpty()) {
                for (CommodityRoyalty commodityRoyalty : selectedCommodity.getCommodityRoyaltyCollection()) {
                    if (selectedDepartmentPost.equals(commodityRoyalty.getDepartmentPost())) {
                        addMessage("添加失败", "不可重复添加", FacesMessage.SEVERITY_WARN);
                        return;
                    }
                }
            }
            CommodityRoyalty commodityRoyalty = new CommodityRoyalty(selectedCommodity, selectedDepartmentPost);
            selectedCommodity.getCommodityRoyaltyCollection().add(commodityRoyalty);
        }
    }

    public String deleteCommodityRoyalty(CommodityRoyalty commodityRoyalty) {
        this.selectedCommodity.getCommodityRoyaltyCollection().remove(commodityRoyalty);
        return null;
    }
}
